package com.derbysoft.lowcostadapter.dswitch2.translator.impl;

import com.derbysoft.lowcostadapter.commons.dto.CancelResponse;
import com.derbysoft.lowcostadapter.commons.dto.CancelResult;
import com.derbysoft.lowcostadapter.dswitch2.translator.Translator;
import com.derbysoft.remote.dswitch.core.GenericRQ;
import com.derbysoft.remote.dswitch.core.GenericRS;
import com.derbysoft.remote.dswitch.core.ResponseHeader;
import com.derbysoft.remote.dswitch.core.SuccessDTO;
import com.derbysoft.remote.dswitch.generic.constants.UniqueIDType;
import com.derbysoft.remote.dswitch.generic.dto.UniqueIDDTO;
import com.derbysoft.remote.dswitch.hotel.dto.CancelInfoDTO;
import com.derbysoft.remote.dswitch.hotel.dto.CancelRQ;
import com.derbysoft.remote.dswitch.hotel.dto.CancelRS;
import org.springframework.stereotype.Component;

@Component("ds2CancelRSTranslator")
public class CancelRSTranslatorImpl implements Translator<CancelResponse, GenericRS<CancelRS>, GenericRQ<CancelRQ>> {
    @Override
    public GenericRS<CancelRS> translate(CancelResponse cancelResponse, GenericRQ<CancelRQ> rq) {
        GenericRS<CancelRS> cancelRSGenericRS = new GenericRS<CancelRS>();
        CancelRS cancelRS = translatePayload(cancelResponse.getCancelResult(), rq);
        cancelRSGenericRS.setPayload(cancelRS);
        cancelRSGenericRS.setSuccess(new SuccessDTO());
        cancelRSGenericRS.setHeader(createHeader(rq));
        return cancelRSGenericRS;
    }

    protected ResponseHeader createHeader(GenericRQ<CancelRQ> rq) {
        ResponseHeader responseHeader = new ResponseHeader();
        responseHeader.setTaskId(rq.getHeader().getTaskId());
        return responseHeader;
    }

    private CancelRS translatePayload(CancelResult cancelResult, GenericRQ<CancelRQ> rq) {
        CancelRS cancelRS = new CancelRS();
        cancelRS.setHotelReservationIDs(rq.getPayload().getHotelReservationIDs());
        cancelRS.setCancelInfo(translateCancelInfo(cancelResult.getCancellationReference()));
        return cancelRS;
    }

    private CancelInfoDTO translateCancelInfo(String cancellationReference) {
        return new CancelInfoDTO(new UniqueIDDTO(cancellationReference, UniqueIDType.CANCELLATION));
    }
}
